<?php

namespace app\common\cachemodel\privatearea;

use app\common\model\BaseCache;
use think\Env;

class PrivateAreaMallCompany extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.private_area_mall_company';

    protected $cacheModel = 'privatearea.company';

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            // 获取未删除 的数据
            $data = $this
                ->where(['id' => ['in', $ids]])
                ->column('id,name,pay_type,ali_pay,wx_pay,aid,create_time,update_time', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'aid', 'create_time', 'update_time']);

            if (isset($itemData['pay_type']))
            {
                $data[$index]['pay_type'] = $itemData['pay_type'] ? json_decode($itemData['pay_type'], true) : [];
            }
            if (isset($itemData['ali_pay']))
            {
                $data[$index]['ali_pay'] = $itemData['ali_pay'] ? json_decode($itemData['ali_pay'], true) : [];
            }
            if (isset($itemData['wx_pay']))
            {
                $data[$index]['wx_pay'] = $itemData['wx_pay'] ? json_decode($itemData['wx_pay'], true) : [];
            }
        }
        return $data;
    }

    /**
     * 获取公司 支付配置
     * @param array $companyData 公司信息
     * @param int $tradeType 支付类型 1：支付宝H5 2：微信H5 3：微信JSAPI
     * @return array
     */
    public function getCompanyPayConfig(array $companyData, int $tradeType)
    {
        if ($tradeType === 1)
        {
            $config = [
                'app_id' => $companyData['ali_pay']['app_id'] ?? '',
                'seller_id' => $companyData['ali_pay']['seller_id'] ?? '',
                'seller_email' => $companyData['ali_pay']['seller_email'] ?? '',
                'notify_url' => $companyData['ali_pay']['notify_url'] ?? '',
                'return_url' => $companyData['ali_pay']['return_url'] ?? '',
                'gatewayUrl' => $companyData['ali_pay']['gatewayUrl'] ?? '',
                'merchant_private_key' => $companyData['ali_pay']['merchant_private_key'] ?? '',
                'merchant_public_key' => $companyData['ali_pay']['merchant_public_key'] ?? '',
                'alipay_public_key' => $companyData['ali_pay']['alipay_public_key'] ?? '',
            ];
        } else
        {
            $config = [
                'appid' => $companyData['wx_pay']['appid'] ?? '',
                'secret' => $companyData['wx_pay']['secret'] ?? '',
                'mchid' => $companyData['wx_pay']['mchid'] ?? '',
                'api_key' => $companyData['wx_pay']['api_key'] ?? '',
                'notify_url' => $companyData['wx_pay']['notify_url'] ?? '',
            ];
        }

        return $config;
    }
}